package com.twu.biblioteca;

import java.util.HashMap;
import java.util.List;

public class Librarian {
    public void displayWelcomeMessage() {
        System.out.println("_________________________________");
        System.out.println(" .....Welcome to Biblioteca..... ");
        System.out.println("_________________________________");
    }

    public void displayMainMenu() {
        System.out.println("1) USER LOGIN");
        System.out.println("2) DISPLAY ALL BOOKS");
        System.out.println("3) DISPLAY ALL MOVIES");
        System.out.println("4) CHECK MEMBERSHIP");
        System.out.println("5) EXIT APPLICATION");
        System.out.println("Enter your option number... ");
    }

    public boolean validateMenuOption(int selectedOption) {
        return ((selectedOption > 0 ) && (selectedOption <= 4));
    }

    public void displayAllBooks() {
        List<LibraryBook> bookList = LibraryRegistry.readBooks();
        for (LibraryBook libraryBook : bookList) {
            System.out.println("* " + libraryBook.getName() + " by " + libraryBook.getAuthor() + " (" + libraryBook.getCopyNumber()
                    + " copy)");
        }
    }

    public void checkMembership(User user) {
        if (user == null)
            System.out.println("Contact librarian in person");
        else
        {
            System.out.println("Name : " + user.getName());
            System.out.println("Email : " + user.getEmail());
        }
    }

    public boolean checkBookAvailability(String name) {
        List<LibraryBook> bookList = LibraryRegistry.readBooks();
        for (LibraryBook book : bookList) {
            if (book.getName().equalsIgnoreCase(name) && (book.getCopyNumber()-book.getReserved() > 0))
                return true;
        }
        return false;
    }

    public boolean reserveBook(String name) {
        List<LibraryBook> bookList = LibraryRegistry.readBooks();
        for (LibraryBook book : bookList) {
            if (book.getName().equalsIgnoreCase(name) && checkBookAvailability(name)) {
                book.setReserved();
                return true;
            }
        }
        return false;
    }

    public void displayBookMenu() {
        System.out.println("1) Check book availability");
        System.out.println("2) Reserve a book");
        System.out.println("Enter your option number... ");
    }

    public void displayAllMovies() {
        List<Movies> moviesList = MovieRegistry.moviesList;
        for (Movies movie : moviesList) {
            System.out.println("* " + movie.getName() + " in " + movie.getReleaseYear() + " directed by " +
                    movie.getDirector() + " -- Rating " + movie.getRating());
        }
    }

    public boolean validateLogin(String userName, String passWord) {
        HashMap<String, String> users = UserRegistry.registeredUsers;
        return (users.containsKey(userName) && users.containsValue(passWord));
    }
}
